package wikisearch;

import java.io.File;
import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;

import wikisearch.similarity.CoOccurenceScoreFunction;
import wikisearch.similarity.CosineScoreFunction;
import wikisearch.testparser.TestParser;
import wikisearch.datastructures.*;

import java.util.*;

/**
 * This class is responsible for the actual 'main' execution of the program.
 * 
 * @author Jason Cho
 *
 */
import wikisearch.cluster.MinCutCluster;
import wikisearch.cluster.SimpleCluster;
import wikisearch.coref.Coreferer;
import wikisearch.datastructures.*;

public class WikiMain {

	public static void main(String [] args) throws IOException {
		
//		run(args);
		Vector<TestParser> vtp = parseFileInFolder("/home/hcho33/cs_stuff/cs410/project/testset");
		runProgram(vtp);
		
	}
	
	public static void runProgram(Vector<TestParser> vtp) {
		
		int windowSize = 10;
		Graph g = new Graph();
		
		for (int i = 0; i < vtp.size(); i++) {
			for (int j = i+1; j < vtp.size(); j++) {
				double currScore = Coreferer.getCoref(vtp.get(i).getString(), vtp.get(i).linksInMap(), vtp.get(i).wordsInMap(), 
						vtp.get(j).getString(), vtp.get(j).linksInMap(), vtp.get(j).wordsInMap(), 
						windowSize, new CosineScoreFunction());//CoOccurenceScoreFunction());

				System.out.println("Score between : " + vtp.get(i).filename + " and " + vtp.get(j).filename + " is " + currScore);
				currScore = ((currScore < 0.00001)? 0.00001 : currScore);
				if (currScore > 0)
					g.setEdgeWeight(vtp.get(i).filename, vtp.get(j).filename, currScore);
			}
		}
		MinCutCluster mcc = new MinCutCluster(g);
		HashMap<Integer, Set<String>> cm = mcc.getClusters();
		for (Integer key : cm.keySet()) {
			System.out.println("Key : " + key + " has " + cm.get(key));
		}
	}
	
	
	public static Vector<TestParser> parseFileInFolder(String file) {
		
		File dir = new File(file);
		Vector<TestParser> vt = new Vector<TestParser>();;
		if (dir.isDirectory()) {
			String [] chld = dir.list();
			for (int i = 0; i < chld.length; i++) {
				vt.addAll( parseFileInFolder(file + "/" + chld[i]) );
			}
		} else {
			TestParser tp = new TestParser(file);
			vt.add(tp);
		}
		return vt;
	}
	
	public static void run(String [] args) throws IOException {
		
		TestParser tp = new TestParser("/home/hcho33/cs_stuff/cs410/project/test_data/dog_wiki.txt");
		TestParser tp2 = new TestParser("/home/hcho33/cs_stuff/cs410/project/test_data/cat_wiki.txt");
		
		double scores = Coreferer.getCoref(tp.getString(), tp.linksInMap(), tp.wordsInMap(), 
							tp2.getString(), tp2.linksInMap(), tp2.wordsInMap(), 10, new CoOccurenceScoreFunction());
		System.out.println("Final score was : " + scores);
		Graph g = new Graph();
		g.setEdgeWeight("dog_wiki", "cat_wiki", scores);
		g.print();
		testCluster();
	}
	
	public static void testCluster() {
		
		Graph g = new Graph();
/*		g.setEdgeWeight("a", "b", 1.0); //cuts minimum edge
		g.setEdgeWeight("b", "c", 7.0);
		g.setEdgeWeight("c", "d", 2.0);
		g.setEdgeWeight("d", "e", 6.0);
		g.setEdgeWeight("e", "f", 3.0);
		g.setEdgeWeight("f", "g", 4.0);
		g.setEdgeWeight("g", "h", 5.0);
		
		SimpleCluster hc = new SimpleCluster(g, 3);
		hc.print();
*/

		g.setEdgeWeight("a", "c", 1.0); 
		g.setEdgeWeight("a", "b", 4.0); 
		g.setEdgeWeight("a", "d", 1.0);
		g.setEdgeWeight("b", "c", 2.0);
		g.setEdgeWeight("b", "d", 2.0); 
		g.setEdgeWeight("c", "d", 5.0); 
		g.setEdgeWeight("d", "e", 2.0); 
		g.setEdgeWeight("c", "e", 3.0); 
		g.setEdgeWeight("e", "f", 6.0); 
		g.setEdgeWeight("a", "g", 1.0); 
		g.setEdgeWeight("c", "g", 2.0); 
		g.setEdgeWeight("g", "h", 3.0); 
		g.setEdgeWeight("g", "i", 4.0); 
		g.setEdgeWeight("i", "h", 5.0); 
		
		
		MinCutCluster mcc = new MinCutCluster(g);
		HashMap<Integer, Set<String>> cm = mcc.getClusters();
		for (Integer key : cm.keySet()) {
			System.out.println("Key : " + key + " has " + cm.get(key));
		}
	}
	/**
	 * Parses the argument
	 * @param args string holding input parameters to the program. 
	 * 			   Provides input file (list of 'nouns' to cluster) and output file (newly clustered nouns).
	 * @return main argument, filled with corresponding args
	 */
	public static MainArguments getArguments(String [] args) {
		
		return new MainArguments();
	}
}
